// pages/info/index.js
const App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatarUrl: "../../images/avatar.png",
      nickName: "银河便利"
    },
    sendTime: '获取验证码',
    sendColor: '#3690EE',
    snsMsgWait: 60,
    phone: '',
    notBuy: false,
    disabled: false,
    sexArray: [{value:'1',name:'男'},{value:'2',name:'女'}],
    sexIndex: 0,
    isCheck:false,
    isAgree:false,
    date: "请选择生日",
    buyPlatform: []
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onLoad(options){
    this.setData(options)
  },
  onShow() {
    this.getUserDetail()
    this.getSourceList()
  },
  //获取来源列表
  getSourceList(){
    App._get('User/Api007', {},  (result)=> {
      let buyPlatform=result.data.map(item=>{
        item.select=false
        return item
      })
      this.setData({
        buyPlatform
      })
    }, false, function () {

    });
  },
  getUserDetail(){
    let userInfo=wx.getStorageSync('userInfo')
    this.setData({
      userInfo,
      sexIndex:userInfo.gender==1?0:1
    })
  },
  // 表单提交
  submitData(e) {
    let _this = this,
      values = e.detail.value
    console.log(values)
    values.uid = values.phone;
    values.uName = this.data.userInfo.nickName;
    values.uSex = this.data.sexArray[this.data.sexIndex].value;
    values.uPic = this.data.userInfo.avatarUrl;
    values.ubirthday = this.data.date
    values.openID =wx.getStorageSync('openid')
    values.sourceList=this.data.buyPlatform.map(item=>{
      if(item.select){
        return item.id
      }
    })
    // 记录formId
    // App.saveFormId(e.detail.formId);
    // 表单验证
    if (!_this.validation(values)) {
      App.showError(_this.data.error);
      return false;
    }

    // 按钮禁用
    _this.setData({
      disabled: true
    });

    // 提交到后端
    App._post_form('User/SetInfo', values, function (result) {
      App.showSuccess(result.msg, function () {
        wx.setStorageSync('token', result.token);
        wx.setStorageSync('openid', result.openID);
        wx.navigateTo({
          url: '/pages/regSuccess/index'
        })
      });
    }, false, function () {
      // 解除禁用
      _this.setData({
        disabled: false
      });
    });
  },
  inputTel: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  inputCode: function(e) {
    this.setData({
      vCode: e.detail.value
    })
  },
  // 获取验证码
  sendCode() {
    if (this.data.phone == "") {
      App.showMsg('请输入手机号');
      return;
    }
    if (!(/^[1][3,4,5,7,8][0-9]{9}$/.test(this.data.phone))) {
      App.showMsg('手机号格式有误');
      return;
    }
    let phone =this.data.phone
    App._get('User/SendCode', {mobile:phone}, (res) => {
      App.showSuccess(res.message);
      // 60秒后重新获取验证码
      let inter = setInterval(function () {
        this.setData({
          smsFlag: true,
          sendColor: '#cccccc',
          sendTime: this.data.snsMsgWait + 's后重发',
          snsMsgWait: this.data.snsMsgWait - 1
        });
        if (this.data.snsMsgWait < 0) {
          clearInterval(inter)
          this.setData({
            sendColor: '#3690EE',
            sendTime: '获取验证码',
            snsMsgWait: 60,
            smsFlag: false
          });
        }
      }.bind(this), 1000);
    })
  },
  //表单验证
  validation: function (values) {
    values.date=this.data.date
    values.isAgree=this.data.isAgree
    if (values.phone.length < 1) {
      this.data.error = '手机号不能为空';
      return false;
    }
    if (values.phone.length !== 11) {
      this.data.error = '手机号长度有误';
      return false;
    }
    if (values.vCode.length < 1) {
      this.data.error = '请填写短信验证码';
      return false;
    }
    let reg =/^[1][3,4,5,7,8][0-9]{9}$/;
    if (!reg.test(values.phone)) {
      this.data.error = '手机号不符合要求';
      return false;
    }
    if (values.date === '请选择生日') {
      this.data.error = '请选择生日';
      return false;
    }
    if (!this.data.isCheck) {
      this.data.error = '请选择您曾经购买过的平台，若没有购买过请选择“还未购买过”';
      return false;
    }
    if (!values.isAgree) {
      this.data.error = '请勾选我已阅读内容';
      return false;
    }

    return true;
  },
  bindSexChange(e) {
    this.setData({

      sexIndex: e.detail.value
    })
  },
  bindDateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  checkboxChange(e) {
    const items = this.data.buyPlatform
    const selectItems=e.detail.value
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].select = selectItems.includes(items[i].id)
    }
    if (selectItems.length > 0) {
      this.setData({notBuy: false})
    }
    this.setData({
      buyPlatform: items,
      isCheck:true
    })
  },
  radioChange(e){
    let buyPlatform = this.data.buyPlatform.map(item => {
      item.select = false
      return item
    })
    this.setData({
      buyPlatform,
      isCheck:true
    })
  },
  agreeCheckboxChange(e){
    this.setData({
      isAgree:e.detail.value
    })
  }
})